#pragma strict
/////////////////////////////////////////////////////////////
//NGUI Tap
/////////////////////////////////////////////////////////////

/** 按钮数组 */
var tapButtons:Transform[];

/** 面板数组 */
var tapPanels:Transform[];

var index:int;
var isForceRefresh:boolean;
//事件名称.
var onSetSelectedEventName:String = "OnSetSelected";

private var selectedIndex:int = -1;

function Start () {
	if(tapPanels.length != tapButtons.length)
		Debug.Log("Tap'Button,Tap'Panel 数量不一致!");
	isForceRefresh = true;
}

function Update () {
	if(selectedIndex != index){
		selectedIndex = index;
		//Debug.Log("update:"+index +":"+gameObject.name);
		ForceRefresh();
	}
	if(isForceRefresh){
		isForceRefresh = false;
		ForceRefresh();
	}
}
function OnEnable(){
	ForceRefresh();
}
function OnDisable(){
	index = 0;
}
function ForceRefresh(){
	//Debug.Log("ForceRefresh:"+index +":"+gameObject.name);
	UpdateButton();
	UpdatePanel();
}

/** TapButton 点击 */
function OnTapButtonClick(args:UIEventArgs){
	if(args){
		var i:int = System.Array.IndexOf(tapButtons,args.transform);
		if(i != -1)
			index = i;
		else
			Debug.LogError("未找到:"+args.transform.name);
	}
}
/** 更新按钮状态 */
private function UpdateButton(){
	for(var i:int=0;i<tapButtons.length;i++){
		var t:Transform = tapButtons[i];
		t.SendMessage(onSetSelectedEventName,i == selectedIndex);
	}
}
/** 更新面板状态 */
private function UpdatePanel(){
	for(var i:int = 0;i<tapPanels.length;i++){
		var t:Transform = tapPanels[i];
		t.gameObject.SetActive(i == selectedIndex);
	}
}